package com.mew.common.rest.bo;

import lombok.*;


@ToString
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class MewSession {

    public long uid = -9;  // userID
    public long tk = -9;   // token
    public int dtp = -9;
    public int gwsid = -9;

    public int loc = -9;    // lob =0 or gid
    public int lsid = -9;   // l sid
    public int gsid = -9;   // g sid
    public int onl = -9;

    public int gid = -9;  // lob =0
    public int rid = -9;
    public int tid = -9;
    public int sno = -9;

    //play info
    public int  pt = -9;   // pl t
    public long mbn = -9;  // g  N  m  t
    public long bt = -9;   // b  t
    public long pmt = -9;  // p  t
    public long mpn = -9;  // max p
    public int  mpm = -9;  // max p m

    public long stm = -9;  // s  b time
    public int otm = -9;   // s o time  [s]

    public long ioid = -9;  // l  id
    public long rioid = -9; // r  id

    public void update(MewSession other){
        if ( -9 != other.tk) {this.tk = other.tk;}
        if ( -9 != other.gwsid) {this.gwsid = other.gwsid;}

        if ( -9 != other.loc) {this.loc = other.loc;}
        if ( -9 != other.lsid) {this.lsid = other.lsid;}
        if ( -9 != other.gsid) {this.gsid = other.gsid;}
        if (  -9 != other.onl) {this.onl = other.onl;}

        if ( -9 != other.gid) {this.gid = other.gid;}
        if ( -9 != other.rid) {this.rid = other.rid;}
        if ( -9 != other.tid) {this.tid = other.tid;}
        if ( -9 != other.sno) {this.sno = other.sno;}

        if ( -9 != other.pt) {this.pt = other.pt;}
        if ( -9 != other.mbn) {this.mbn = other.mbn;}
        if ( -9 != other.bt) {this.bt = other.bt;}
        if ( -9 != other.pmt) {this.pmt = other.pmt;}
        if ( -9 != other.mpn) {this.mpn = other.mpn;}
        if ( -9 != other.mpm) {this.mpm = other.mpm;}

        if ( -9 != other.stm) {this.stm = other.stm;}
        if ( -9 != other.otm) {this.otm = other.otm;}

        if ( -9 != other.ioid) {this.ioid = other.ioid;}
        if ( -9 != other.rioid) {this.rioid = other.rioid;}
    }


//    public void exitGame(long token){
    public void exitGame(){
//        this.tk = token;
        this.loc = 0;
        this.gsid = -9;

        this.gid = -9;
        this.rid = -9;
        this.tid = -9;
        this.sno = -9;

        //play info
        this.pt = -9;
        this.mbn = -9;
        this.bt = -9;
        this.pmt = -9;
        this.mpn = -9;
        this.mpm = -9;

        this.stm = -9;
        this.otm = -9;

        this.ioid = -9;
        this.rioid = -9;
    }

    public void initPlayInfo(){
        this.pt = 0;
        this.mbn = 0;
        this.bt = 0;
        this.pmt = 0;
        this.mpn = 0;
        this.mpm = 0;
    }

    public void incrementPayment(long goldNumber, long betNumber, long paymentNumber, int multiple){
        if (goldNumber > this.mbn){
            this.mbn = goldNumber;
        }
        this.bt += betNumber;
        this.pmt += paymentNumber;
        if (paymentNumber > this.mpn){
            this.mpn = paymentNumber;
        }
        if (multiple > this.mpm){
            this.mpm = multiple;
        }

        ++this.pt;
        this.stm = -9;
        this.otm = -9;
    }
    public void chessBetLock(int overTime){
        this.stm = System.currentTimeMillis();
        this.otm = overTime;
    }

    /** true playing, false: not */
    public boolean checkChessPlaying(){
        if (this.otm == -9 || this.stm == -9){
            return false;
        }
        if (remainTime() < 1){
            return false;
        }
        return true;
    }


    public int remainingLock(){
        if (!checkChessPlaying()){
            return 0;
        }
       return remainTime();
    }

    private int remainTime(){
        long currentTime = System.currentTimeMillis();
        int remain = (int) ((currentTime - this.stm)/1000);
        return this.otm - remain;
    }

    public void setLockTime(long startTime, int otm){
        this.stm = startTime;
        this.otm = otm;
    }



}
